﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;

namespace SLControlLibrary.Resources.Controls.Calendar
{
    public class Drag_ResizeManager
    {
        #region Variables
        private bool Tracking = false;
        private bool Dragging = false;
        private bool Resizing = false;
        private Point LastDragXY;
        private Panel Root;
        private UIElement Element;
        private bool IsTopTenY = false;
        private bool IsBottomTenY = false;
        #endregion

        #region Properties
        public bool XResizable { get; set; }
        public bool YResizable { get; set; }
        #endregion

        #region Constructor
        public Drag_ResizeManager(Panel Root, bool CanResizeX, bool CanResizeY)
        {
            this.Root = Root;
            XResizable = CanResizeX;
            YResizable = CanResizeY;
        }
        #endregion

        #region Methods
        #region AddDragResizeItem
        public void AddDragResizeItem(UIElement NewElement)
        {
            this.Element = NewElement;
            this.Element.MouseLeftButtonDown += DragItem_MouseLeftButtonDown;
            this.Element.MouseMove += DragItem_MouseMove;
            this.Element.MouseLeftButtonUp += DragItem_MouseLeftButtonUp;
        }
        #endregion

        #region RemoveDragResizeItem
        public void RemoveDragResizeItem()
        {
            Element.MouseLeftButtonDown -= DragItem_MouseLeftButtonDown;
            Element.MouseMove -= DragItem_MouseMove;
            Element.MouseLeftButtonUp -= DragItem_MouseLeftButtonUp;
        }
        #endregion

        #region GetTranslateTransform
        private TranslateTransform GetTranslateTransform(UIElement items)
        {
            TranslateTransform tt = null;

            if (items.RenderTransform is TranslateTransform)
                tt = items.RenderTransform as TranslateTransform;
            else if (items.RenderTransform is TransformGroup)
            {
                TransformGroup tg = items.RenderTransform as TransformGroup;
                foreach (GeneralTransform gt in tg.Children)
                {
                    if (gt is TranslateTransform)
                        tt = (TranslateTransform)gt;
                }
            }
            else
            {
                tt = new TranslateTransform();
                items.RenderTransform = tt;
            }

            return tt;
        }
        #endregion

        #region GetScaleTransform
        private ScaleTransform GetScaleTransform(UIElement items)
        {
            ScaleTransform st = null;

            if (items.RenderTransform is ScaleTransform)
                st = items.RenderTransform as ScaleTransform;
            else if (items.RenderTransform is TransformGroup)
            {
                TransformGroup tg = items.RenderTransform as TransformGroup;
                foreach (GeneralTransform gt in tg.Children)
                {
                    if (gt is ScaleTransform)
                        st = (ScaleTransform)gt;
                }
            }
            else
            {
                st = new ScaleTransform();
                items.RenderTransform = st;
            }

            return st;
        }
        #endregion
        #endregion

        #region Events
        #region DragItem_MouseLeftButtonUp
        private void DragItem_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            (sender as UIElement).ReleaseMouseCapture();
            Tracking = false;
            Resizing = false;
            Dragging = false;
            IsTopTenY = false;
            IsBottomTenY = false;
            ucContentControl item = (sender as ucContentControl);
            item.Opacity = 1;
            item.Cursor = Cursors.Arrow;
        }
        #endregion

        #region DragItem_MouseMove
        private void DragItem_MouseMove(object sender, MouseEventArgs e)
        {
            ucContentControl item = (ucContentControl)sender;    
            Point currentDragXY = e.GetPosition(Root);
            bool CanResize = false;

            if (!Resizing && !Dragging)
            {
                Point currentResizeXY = e.GetPosition(item);

                if (YResizable && (currentResizeXY.Y <= 5 || currentResizeXY.Y >= (item.ActualHeight * .98)) && item.ActualHeight > 22)
                {
                    if (currentResizeXY.Y <= item.ActualHeight * .02)
                        IsTopTenY = true;
                    else if (currentResizeXY.Y >= (item.ActualHeight * .98))
                        IsBottomTenY = true;

                    CanResize = true;
                    item.Cursor = Cursors.SizeNS;
                }

                //if (XResizable && (currentResizeXY.X <= 10 || currentResizeXY.X >= item.ActualWidth - 10))
                //{
                //    if (currentResizeXY.X <= 10)
                //        IsTopTenX = true;
                //    else if (currentResizeXY.X >= item.ActualWidth - 10)
                //        IsBottomTenX = true;

                //    CanResize = true;
                //    item.Cursor = Cursors.SizeWE;
                //}

                if (!CanResize && !Resizing && !Dragging)
                    item.Cursor = Cursors.Arrow;
            }
            else
            {
                if (!Dragging && Resizing && item.ActualHeight > 22)
                    CanResize = true;
            }

            if (Tracking)
            {
                if (CanResize)
                {
                    Resizing = true;

                    if (IsTopTenY)
                    {
                        if (currentDragXY.Y > (LastDragXY.Y + 10) && (item.ActualHeight - 22) > 22)
                        {
                            item.Resize(new Size(item.ActualWidth, item.ActualHeight - (double)22));
                            LastDragXY = currentDragXY;
                            TranslateTransform tt = GetTranslateTransform(item);
                            tt.Y += 11;
                        }
                        else if (currentDragXY.Y < (LastDragXY.Y - 10))
                        {
                            item.Resize(new Size(item.ActualWidth, item.ActualHeight + (double)22));
                            LastDragXY = currentDragXY;
                            TranslateTransform tt = GetTranslateTransform(item);
                            tt.Y -= 11;
                        }
                    }
                    else if (IsBottomTenY)
                    {
                        if (currentDragXY.Y > (LastDragXY.Y + 10))
                        {
                            item.Resize(new Size(item.ActualWidth, item.ActualHeight + (double)22));
                            LastDragXY = currentDragXY;
                            TranslateTransform tt = GetTranslateTransform(item);
                            tt.Y -= 11;
                        }
                        else if (currentDragXY.Y < (LastDragXY.Y - 10) && (item.ActualHeight - 22) > 22)
                        {
                            item.Resize(new Size(item.ActualWidth, item.ActualHeight - (double)22));
                            LastDragXY = currentDragXY;
                            TranslateTransform tt = GetTranslateTransform(item);
                            tt.Y += 11;
                        }
                    }
                }
                else 
                {
                    Dragging = true;

                    TranslateTransform tt = GetTranslateTransform(item);
                    item.Opacity = .45;

                    if (currentDragXY.Y > (LastDragXY.Y + 10))
                    {
                        tt.Y += 22;
                        LastDragXY = currentDragXY;
                    }
                    else if (currentDragXY.Y < (LastDragXY.Y - 10))
                    {
                        tt.Y -= 22;
                        LastDragXY = currentDragXY;
                    }
                }
            }
        }
        #endregion

        #region DragItem_MouseLeftButtonDown
        void DragItem_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Tracking = true;
            LastDragXY = e.GetPosition(Root);
            (sender as UIElement).CaptureMouse();
        }
        #endregion
        #endregion
    }
}
